﻿using HslCommunication.ModBus;
using Prism.Events;
using RoboAssembleX.Application.AddGlass;
using RoboAssembleX.Application.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace RoboAssembleX.Application.AddNeedle1
{
    public class AddNeedle1Service
    {
        public AddNeedle1Service(AddNeedle1Model AddNeedle1Model, IEventAggregator eventAggregator)
        {
            this.AddNeedle1Model = AddNeedle1Model;
            this.eventAggregator = eventAggregator;
        }

        #region ReadData
        ModbusTcpNet modbus;
        Task monitorDataTask;
        System.Timers.Timer timer;
        private readonly AddNeedle1Model AddNeedle1Model;
        private readonly IEventAggregator eventAggregator;

        public void SwitchReadData()
        {
            if (AddNeedle1Model.ReadDataFlag)
            {

                timer?.Stop();
                timer?.Dispose();
                AddNeedle1Model.ReadDataFlag = false;
                eventAggregator.GetEvent<MessageEvent>().Publish(new MessageEventArgs("精切引线装配设备1断开成功!", 1));
            }
            else
            {
                modbus = new ModbusTcpNet("192.168.0.105", 502);
                modbus.AddressStartWithZero = true;
                modbus.DataFormat = HslCommunication.Core.DataFormat.CDAB;
                var r = modbus.ConnectServer();
                if (r.IsSuccess)
                {
                    monitorDataTask = new Task(ReadDataJob);
                    monitorDataTask.Start();
                    timer = new System.Timers.Timer(1000);
                    timer.Elapsed += OnTimerElapsed;
                    timer.Start();
                    AddNeedle1Model.ReadDataFlag = true;
                    eventAggregator.GetEvent<MessageEvent>().Publish(new MessageEventArgs("精切引线装配设备1连接成功!", 1));
                }
                else
                {
                    AddNeedle1Model.ReadDataFlag = false;
                    eventAggregator.GetEvent<MessageEvent>().Publish(new MessageEventArgs("精切引线装配设备1连接失败!", 1));
                }
            }
        }
        void ReadDataJob()
        {
            while (AddNeedle1Model.ReadDataFlag)
            {
                var result = modbus.ReadBool("500", 3);
                if (result.IsSuccess)
                {
                    AddNeedle1Model.AllowRemove = result.Content[0];
                    AddNeedle1Model.AllowPut = result.Content[1];
                    AddNeedle1Model.PosUsed = result.Content[2];
                    AddNeedle1Model.Running = !result.Content[0] && !result.Content[1];
                }
            }
        }
        #endregion

        public void Action()
        {
            if (!AddNeedle1Model.Running)
            {
                var result = modbus.Write("9", true);
            }
        }
        private void OnTimerElapsed(object sender, ElapsedEventArgs e)
        {
            // 根据Run属性的值决定增加A还是B
            if (AddNeedle1Model.Running)
            {
                AddNeedle1Model.RunTimeCount += 1;
            }
            else
            {
                AddNeedle1Model.StopTimeCount += 1;
            }
        }
    }
}
